package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentGuideRespondBinding;
import com.jwdf.uda.event.GuideCancelEvent;
import com.jwdf.uda.event.GuideDetailEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.GuideCancelAction;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ToastUtils;
import com.jwdf.uda.view.dialog.ConfirmDialog;

import retrofit2.Response;

/**
 * 类说明：查看响应情况
 */
public class GuideRespondActivity extends AbsTitleBarActivity{
    private final static String EXTRA_GUIDEID = "guideId";
    private ConfirmDialog mCancelReqDialog;
    private int guideId;
    public static void startActivity(Context context, int guideId){
        Intent intent = new Intent(context, GuideRespondActivity.class);
        intent.putExtra(EXTRA_GUIDEID, guideId);
        context.startActivity(intent);
    }
    @Override
    protected String getTopTitle() {
        return getString(R.string.title_guide_respond);
    }
    @Override
    protected BaseFragment onCreateContentFragment() {
        return new GuideRespondFragment();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        guideId = getIntent().getIntExtra(EXTRA_GUIDEID, 0);
        super.onCreate(savedInstanceState);
    }
    @Override
    protected void setupTitleView() {
        super.setupTitleView();
        setOperateTitle(getString(R.string.guide_respond_cancel));
    }
    @Override
    public void onHeadActionClick(View view) {
        super.onHeadActionClick(view);
        showCancelReqDialog();
    }
    private void showCancelReqDialog(){
        if(mCancelReqDialog == null){
            mCancelReqDialog = new ConfirmDialog(this, getString(R.string.dialog_toast_cancel_guide_request), getString(R.string.guide_respond_cancel_btn), getString(R.string.guide_respond_sure_btn));
            mCancelReqDialog.setCallbackListener(new ConfirmDialog.CallbackListener() {
                @Override
                public void clickNo() {

                }

                @Override
                public void clickYes() {
                    doCancelRequestGuideAction();
                }
            });
        }
        mCancelReqDialog.show();
    }
    private void doCancelRequestGuideAction(){
        showProgressDialog();
        onNewRequestCall(GuideCancelAction.newInstance(this, guideId).request(new AHttpService.IResCallback<PbBean.PbResGuideCancel>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResGuideCancel> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (!isFinishing()) {
                    closeProgressDialog();
                    if (HttpHelper.showError(GuideRespondActivity.this, resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doCancelRequestGuideSuccess();
                }
            }
        }));
    }
    private void doCancelRequestGuideSuccess() {
        ToastUtils.show(this, R.string.toast_cancel_success);
        finish();
        EventBusUtil.post(new GuideDetailEvent(GuideDetailEvent.EVENT_REQ_CANCEL));
        EventBusUtil.post(new GuideCancelEvent(guideId));
    }
    public static class GuideRespondFragment extends BaseFragment{
        private FragmentGuideRespondBinding mBinding;
        private AnimationDrawable animator;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_guide_respond, container, false);
            initView();
            return mBinding.getRoot();
        }

        private void initView() {
            animator = (AnimationDrawable)mBinding.ivLoadRespond.getBackground();
            animator.start();
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            if(animator != null){
                animator.stop();
            }
        }
    }

}
